package Lee_code;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Lee_350 {

    public int[] intersect(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> seen1 = new HashMap<>();
        HashMap<Integer, Integer> seen2 = new HashMap<>();

        for(int i: nums1)
        {
            int fre = seen1.getOrDefault(i, 0);
            seen1.put(i,fre+1);
        }
        for(int i: nums2)
        {
            int fre = seen2.getOrDefault(i, 0);
            seen2.put(i,fre+1);
        }

        List<Integer> list = new LinkedList<>();

        for(int i: seen1.keySet())
        {
            if(seen2.get(i)!=0)
            {
                int freq = Math.min(seen1.get(i), seen2.get(i));
                for(int j=0; j<freq; j++)
                    list.add(i);
            }
        }
        int[] result = new int[list.size()];
        int sign=0;
        for(int i: list)
        {
            result [sign] = i;
            sign++;
        }
        return  result;
    }
    public static void main(String[] args) {

    }
}
